#include "msp430x20x3.h"
#include "software_serial.h"

void begin(void)
{
    //TimerA_UART_init(2400);                     // Start Timer_A UART
    //TimerA_UART_init(4800);                     // Start Timer_A UART
    TimerA_UART_init(9600);                     // Start Timer_A UART

    TimerA_UART_print("MSP430F2013 TimerA UART\r\n");
    TimerA_UART_print("READY.\r\n");
}
void loop(void)
{
    // Wait for incoming character
    unsigned char buff = TimerA_UART_rx();
    
    // Update board outputs according to received byte
    if (buff & 0x01) P1OUT |= 0x01; else P1OUT &= ~0x01;    // P1.0
    if (buff & 0x02) P1OUT |= 0x08; else P1OUT &= ~0x08;    // P1.3
    if (buff & 0x04) P1OUT |= 0x10; else P1OUT &= ~0x10;    // P1.4
    if (buff & 0x08) P1OUT |= 0x20; else P1OUT &= ~0x20;    // P1.5
    if (buff & 0x10) P1OUT |= 0x40; else P1OUT &= ~0x40;    // P1.6
    if (buff & 0x20) P1OUT |= 0x80; else P1OUT &= ~0x80;    // P1.7
    if (buff & 0x40) P2OUT |= 0x40; else P2OUT &= ~0x40;    // P2.6
    if (buff & 0x80) P2OUT |= 0x80; else P2OUT &= ~0x80;    // P2.7
        
    // Echo received character
    TimerA_UART_tx(buff);
}

//------------------------------------------------------------------------------
// main()
//------------------------------------------------------------------------------
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;               // Stop watchdog timer

    DCOCTL = 0x00;                          // Set DCOCLK to 1MHz
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;

    P1OUT = 0x00;                           // Initialize all GPIO
    P1SEL = UART_TXD + UART_RXD;            // Timer function for TXD/RXD pins
    P1DIR = 0xFF & ~UART_RXD;               // Set all pins but RXD to output
    P2OUT = 0x00;
    P2SEL = 0x00;
    P2DIR = 0xFF;

    __enable_interrupt();

    begin();
    while (1) {
        loop();
    }
}
